#include "recipescreen.h"
#include "ui_recipescreen.h"
#include <QDebug>
#include <QSqlQuery>
#include <QMessageBox>
#include "search.cpp"

RecipeScreen::RecipeScreen(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::RecipeScreen)
{
    ui->setupUi(this);
}

RecipeScreen::~RecipeScreen()
{
    delete ui;
}

void RecipeScreen::on_searchButton_clicked()
{
    QString userQuery(this->ui->searchBox->text());
    QStringList tokens = userQuery.split(' ');

    this->ui->listWidget->clear();
    for(int j=0;j<tokens.size();j++)
        tokens.at(j).toLower();

    QSqlQuery query;
    if(!query.exec("SELECT name FROM Recipe"))
        qDebug()<<"invalid query";
    int i=0;
    bool found = false;
    while (query.next()) {
        QString name = query.value(0).toString();
        name.toLower();
        for(int j=0;j<tokens.size();j++){
            if(name.contains(tokens.at(j)))
                found=true;
        }
        if(found)
            this->ui->listWidget->insertItem(i++,name);
        found = false;
    }
    if(i==0)
        QMessageBox::critical(0, qApp->tr("No se encontraron recetas!"),
                              qApp->tr("No se encontraron recetas que coincidan con su busqueda, intente usar palabras mas genericas o menos palabras"),
                              QMessageBox::Ok);
    this->ui->stackedWidget->setCurrentIndex(1);
}

void RecipeScreen::on_listWidget_doubleClicked(const QModelIndex &index)
{
    qDebug()<<"Double clicked a recipe";
    QString name=ui->listWidget->currentItem()->text();
    QSqlQuery query;
    query.exec("SELECT * FROM Recipe WHERE name=\""+name+"\"");
    query.next();
    Recipe *recipe = new Recipe();
    int id = query.value(0).toInt();
    recipe->name = query.value(1).toString();
    recipe->type = query.value(2).toString();
    recipe->preparation = query.value(3).toString();
    recipe->userScore = query.value(4).toInt();
    recipe->vegetarian = query.value(5).toBool();
    recipe->dificulty = query.value(6).toString();
    recipe->time = query.value(7).toString();
    recipe->cal_level = query.value(8).toString();
    recipe->serves = query.value(9).toInt();
    this->recipe = recipe;

    this->ui->nameLabel->setText(this->recipe->name);
    this->ui->prepText->setText(recipe->preparation);
    QString details = "Tipo: "+recipe->type+'\n';
    details += recipe->userScore + "Estrellas \n";
    details += "Tiempo de preparacion: "+recipe->time+'\n';
    details += "Dificultad: "+recipe->dificulty+'\n';
    if(recipe->vegetarian)
        details += "Vegetariano: Si \n";
    else
        details += "Vegetariano: No \n";
    details += "Calorias: "+recipe->cal_level+'\n'+'\n';
    if(recipe->serves!=0)
        details +="Ingrecientes para "+QString::number(recipe->serves)+" personas\n";
    details += "INGREDIENTES:\n";

    if(!query.exec("SELECT verbose_name FROM Ingredient WHERE recipe_id="+QString::number(id)+";"))
    {
        qDebug()<<"Failed to get ingredients";
    }
    while(query.next())
    {
        details+="-"+query.value(0).toString()+"\n";
    }
    this->ui->detailsText->setText(details);
    this->ui->stackedWidget->setCurrentIndex(0);

}

void RecipeScreen::on_HomeButton_clicked()
{
    emit change_index(0);
}

void RecipeScreen::on_WhatToEat_clicked()
{

    //generate sugestions and fill the list
    this->ui->listWidget->clear();
    this->ui->listWidget->addItems(search());
    emit change_index(1);
    this->ui->stackedWidget->setCurrentIndex(1);
}
